<template>
  <div class="home-container">

    <!-- 顶部导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        @click="$router.push('/search')"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->

<!-- Tab标签页 -->
    <van-tabs v-model="active" class="channel-tabs" animated swipeable>
      <van-tab :title="obj.name" v-for="obj in channels" :key="obj.id">
        <!-- //文章列表组件 -->
        <!-- 每个选项都有自己的列表，所以将列表封装成组件 -->
        <ArticleList :channel='obj'>
        </ArticleList>
      </van-tab>
      <div slot="nav-right" class="slot"></div>
      <!-- 汉堡按钮 -->

      <div slot="nav-right" class="hamburger-btn" @click="isChannelEditShow=true">
        <div style="width:1px" class="hanbao"></div>
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
<!-- Tab标签页 -->

    <!-- 弹层 -->
    <van-popup v-model="isChannelEditShow" position="bottom"
     closeable
     close-icon-position="top-left"
    :style="{ height: '100%' }">
    <!-- 弹层内容 -->
      <Channel :myChannels='channels' :active='active'
      @updataActive='updataActive'/>
    </van-popup>
    <!-- 弹层 -->

  </div>
</template>

<script >
// 导入频道请求方法
import ArticleList from '@/views/home/components/article.vue'
import { getUserChannels } from '@/api/user'
import Channel from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  components: {
    ArticleList,
    Channel

  },
  created () {
    this.locagetChannels()
  },

  name: 'homeIndex',
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 发出请求频道请求
    async locagetChannels () {
      try {
        if (this.user) {
          // 如果已登录
          const { data } = await getUserChannels()
          // 拿到数据（频道列表）
          this.channels = data.data.channels
        } else {
          // 如果未登录
          if (getItem('TOUTIAO_CHANNELS')) {
            // 本地有存储
            this.channels = getItem('TOUTIAO_CHANNELS')
          } else {
            // 如果本地没有存储就请求默认接口
            const { data } = await getUserChannels()
            // 拿到数据（频道列表）
            this.channels = data.data.channels
          }
        }
      } catch (err) {
        // 提示用户错误
        this.$toast('获取用户频道失败')
      }
    },
    showPopup () {
      this.isChannelEditShow = true
    },

    // es6默认参数的写法，可以给参数赋值
    updataActive (index, isChannelEditShow) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }

  }
}
</script>

<style scoped lang="less">
  .home-container {
    padding-top:174px;
    padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    //background-color: #050608;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }

  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #EDEFF3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }
     .slot{
          //不使用felx的分布
          flex-shrink: 0;
          width: 66px;
          height: 86px;
        }
    .hamburger-btn{
       position: fixed;
        right: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 66px;
        height: 82px;
        background-color: #fff;
        opacity: 0.902;
        i.toutiao {
          font-size: 33px;
        }

        .hanbao {
          position: absolute;
          left: 0;
          height: 100%;
          background: linear-gradient(transparent 0% ,
           transparent 25%, #ccc 26% , #999 50% , #ccc 75% , transparent 76% , transparent 100%) ;
        }
    }
    .van-tab--active .van-tab__text{
      color: #333 !important;
    }
    .van-tabs__line {
      width:31px !important;
      height:6px;
      background:rgba(50,150,250,1);
      border-radius:3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .van-tabs__wrap{
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right:0%;
    }
  }

}
</style>
